using DanMuHelper.Logging;
using System;
using UnityEngine;

namespace DanMuHelper.Audio
{
    public class AudioObject : MonoBehaviour
    {
        public delegate void OnComplete();

        public AudioSource audioSource;
        public float playStartTime;
        public bool playStarted;
        public OnComplete onComplete;

        // public float duration;
        private Transform _transform;

        private void Awake()
        {
            _transform = transform;
            audioSource = GetComponent<AudioSource>();
            audioSource.playOnAwake = false;
            if (audioSource.isPlaying)
                audioSource.Stop();
        }

        #region Clip

        /// <summary>
        /// 单次播放
        /// </summary>
        public void PlayClip(AudioClip clip, bool mute, bool isLoop = false, float volumeScale = 1.0F)
        {
            if (audioSource.mute != mute)
                audioSource.mute = mute;
            // duration = clip.length;
            audioSource.clip = clip;
            audioSource.loop = isLoop;
            audioSource.Play();
            playStartTime = Time.unscaledTime;
            playStarted = true;
            DebugAudioState("Playing");
        }

        /// <summary>
        /// 单次播放, 3d音效, 跟随位置
        /// </summary>
        public void PlayClip(AudioClip clip, bool mute, Vector3 position, bool isLoop = false, float volumeScale = 1.0F)
        {
            _transform.position = position;
            PlayClip(clip, mute, isLoop, volumeScale);
        }

        #endregion

        #region OneShot TODO

        /// <summary>
        /// 单次播放
        /// </summary>
        public void PlayOneShot(AudioClip clip, bool mute, float volumeScale = 1.0F)
        {
            if (audioSource.mute != mute)
                audioSource.mute = mute;
            // duration = clip.length;
            audioSource.PlayOneShot(clip, volumeScale);
            playStartTime = Time.unscaledTime;
            playStarted = true;
            DebugAudioState("Playing");
        }

        /// <summary>
        /// 单次播放, 3d音效, 跟随位置
        /// </summary>
        public void PlayOneShot(AudioClip clip, bool mute, Vector3 position, float volumeScale = 1.0F)
        {
            _transform.position = position;
            PlayOneShot(clip, mute, volumeScale);
        }

        #endregion

        [Obsolete("TODO")]
        public void PlayDelayed(AudioClip clip, float delay) { }

        [Obsolete("TODO")]
        public void PlayScheduled(AudioClip clip, double time) { }

        /// <summary>
        /// 3d音效，设置音源位置
        /// </summary>
        [Obsolete("TODO")]
        public void UpdatePosition(Transform followPoint)
        {
            //_transform.position = position;
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            playStarted = false;
            if (audioSource.isPlaying)
            {
                DebugAudioState("Stopped");
                audioSource.loop = false;
                audioSource.Stop();
            }
        }

        /// <summary>
        /// 静音
        /// </summary>
        public void Mute()
        {
            if (!audioSource.mute)
                audioSource.mute = true;
        }

        /// <summary>
        /// 解除静音
        /// </summary>
        public void Unmute()
        {
            if (audioSource.mute)
                audioSource.mute = false;
        }

        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void DebugAudioState(string newState)
        {
            //Color.blue.LogInfo(newState);
        }
    }
}
